﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.DesignScript.Geometry;

namespace DeepSpace.Analysis
{
    public class VisibilityAnalysis
    {
        public static IList<int> GetVisiblePointNumber(IList<Point> points, Geometry geometry)
        {
            int count = points.Count();
            var visibilities = Enumerable.Repeat(1, count).ToList();

            for (int i = 0; i < count; ++i)
            {
                var pi = points[i];

                for (int j = i + 1; j < count; ++j)
                {
                    var pj = points[j];
                    var line = Line.ByStartPointEndPoint(pi, pj);

                    var intersects = geometry.Intersect(line);
                    if (intersects.Count() != 0)
                    {
                        continue;
                    }

                    var intersectedLine = intersects[0] as Line;
                    if (intersectedLine == null)
                    {
                        continue;
                    }

                    // pi and pj can see each other
                    if (line.Length <= intersects.Length)
                    {
                        visibilities[i] = visibilities[i] + 1;
                        visibilities[j] = visibilities[j] + 1;
                    }
                }
            }

            return visibilities;
        }
    }
}
